<?php
// 1 PostScript point = 1/72 inches = 25.4/72 mm = 0.3527 mm)
//. 12 points to the pica
/*if (strstr($_SERVER['REQUEST_URI'],'nul.php')){
header('HTTP/1.0 404 Not Found');
echo "0000000000000";
 * }*/
//$out=array_map(function($x) {return $x*$x;}, range(0, 9))
//haskell: thread ring programs
//usr fills rows or columns of records
////every record is stored as a paep file
//calls met params naar random image op web voor logo + file structure
//globals: $CONTEXT,$TAG1,$NOIMG,$CRLF;
//include "babsym.php";

global $CRLF; $CRLF='<br />';
//function crlf($crlf='<br />'){ return $crlf; }
$TAG1=' />';
//$NOIMG=perr('No Image');
$CONTEXT=array();

//function pdoc($i){}
function pbr(){ echo'<br />'; 	}
function perr($s){ echo '<pre>',$s,'</pre>'; }
function berr($s){
    $err=array('<pre>',$s,'</pre>');
    $tag=implode('',$err); return $tag;
}

function pstop($ps=''){ echo'</body></html>'; }
function pstart($title='',$meta='',$css='',$js=''){
  $doctypes=array(
  '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
  '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
  );
  echo'<html xmlns="http://www.w3.org/1999/xhtml"><head>';
  if(is_array($css)){
    foreach ($css as &$sheet){ echo'\n @import url("',$sheet,'")'; }
  }else{
    echo'<style type="text/css" media="all">\n',$css,'</style>'; }
  if(is_array($js)){
    foreach ($js as &$script){ echo'<script type="text/javascript" src="',
      $script,'"></script>'; }
  }
//<link type="text/css" rel="StyleSheet" href="css/winclassic.css" />
  echo'<title>'.$title.'</title></head><body>';
  //	<link href="style-.css" rel="stylesheet" type="text/css" media="screen" />
}

function bstop($ps=''){ return'</body></html>'; }
function bstart($title='',$meta='',$css='',$scripts=''){
//<link type="text/css" rel="StyleSheet" href="css/winclassic.css" />
  $doctypes=array(
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
	);
  if(is_array($css)){
    foreach($css as &$sheet){
	  $sheet=' @import url("'.$sheet.'")';
	}
	$css='<style type="text/css" media="all">'.implode('',$css).'</style>';
  }else{ $css=''; }
  if(is_array($scripts)){
	foreach($scripts as &$js){
	  $js='<script type="text/javascript" src="'.$js.'"></script>';
	}
	$scripts=implode('',$scripts);
  }else{ $scripts=''; }
  $head=array($doctypes[0],'<html>','<head>','<title>',$title,'</title>',$css,$scripts,'</head><body>');
  $tag=implode('',$head);
  return $tag;
}


function bscript($ps){
    $iframe=array('<script type="text/javascript">//<![CDATA[',$ps,'//]]></script>');
    $tag=implode('',$iframe);
    return $tag;
}

function btable($pa){
  $CRLF='<br />';
  $table=array('<table border=1>');
  foreach ($pa as $value) {
    if ($value==$CRLF){
	  if (count($table)==1){ $table=array('<table border=1><tr>'); }
	  else { $table[]='</tr><tr>';}
	}else{ $table[]='<td>'.$value.'</td>'; }}
	$table[]='</tr></table>';
    return $table;
}




function bnotempty($ps){ //return true if not empty
   if($ps<>''){ return true; } else { return false; }
}

function bform($contents,$script='form.php',$encoding=''){
	if (is_array($contents)){ $contents=implode('',$contents); }
	if ($encoding<>'') { $encoding=' enctype="' . $encoding . '" '; }
    $form=array('<form',$encoding ,'action="',$script,'" method="post">',$contents,'</form>');
    $tag=implode('',$form);
    return $tag;
}
function pform($contents,$script='form.php',$method='post',$encoding=''){
  if(is_array($contents)){ $contents=implode('',$contents); }
  if($encoding<>'') { $encoding=' enctype="' . $encoding . '" '; }
  echo'<form',$encoding ,(bnotempty($script)?' action="'.$script.'"':''),
  ' method="post">',$contents,'</form>';
}
function plabel($ps){ echo'<label>',$ps,'</label>'; }
function binput($name,$type,$value='',$label=''){
  if ($label<>''){ $label='<label>' . $label . '</label>'; }
  $value='" value="' . $value;
  $input=array($label,'<input name="',$name,'" type="',$type,$value,'" />');
  return $input;
}
function pinput($type,$name,$value='',$label=''){
  if ($label<>''){ echo'<label>',$label,'</label>'; }
  echo$label,'<input name="',$name,'" type="',$type,'" value="',$value,'" />';
}

function jslib($libname){
  switch($libname){
    case'jquery':return'http://code.jquery.com/jquery.js'; break;
	case'jquerymin':return'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'; break;
	case'jclipboardmin':return'../js/jquery.clipboard.min.js'; break;
	case'jclipboard':return'../js/jquery.clipboard.js'; break;
	case'xx':return''; break;
    case'scapi':return'../js/soundcloud.player.api.js'; break;
    case'scplayer':return'../js/sc-player.js'; break;
	default: return'';
  }
}

function jslibs($libs){
  $c=count($libs);
  for($i=0; $i<$c; ++$i){
    $libs[$i]='<script type="text/javascript" src="'.jslib($libs[$i]).'"></script>';
  }
  return implode($libs);
//  echo "$input[0] + $input[1] = ", $input[0]+$input[1];
}

function pselect($pa,$name='',$selected='',$class='',$id=''){//todo option classing with array params
  $options=array();
  foreach($pa as $option){
    if($option==$selected){ $options[]=boption($option,$option,true);
	}else{ $options[]=boption($option,$option); }
  }
  echo'<select',
    (bnotempty($name)?' name="'.$name.'"':''),
	(bnotempty($class)?' class="'.$class.'"':''),
	(bnotempty($id)?' id="'.$id.'"':''),
  '>',implode($options),'</select>';
}

function poption($ps,$value=''){
    if ($value<>'') { $value=' value="' . $value . '" '; }
    $option=array('<option ',$value,'>',$ps,'</label>');
    $tag=implode('',$option);
    return $tag;
}
function boption($ps,$value='',$selected=''){
    if ($value<>'') { $value=' value="' . $value . '" '; }
    $option=array('<option ',$value,(bnotempty($selected)?' selected="selected"':''),
	  '>',$ps,'</option>');
    $tag=implode('',$option);
    return $tag;
}

function htag($ps,$tag){ return '<'.$tag.'>'.$ps.'</'.$tag.'>'; }

function btag($data,$funk,$prefix='',$suffix=''){
  global $CRLF;
  $tags=array();
  foreach($data as $datum){
    if($datum==$CRLF){$tags[]=$datum;
    }else{
      $cmd=$prefix.$datum.$suffix;
	  $result=call_user_func($funk,$cmd);
	  if(is_array($result)){ $tags[]=implode($result);
	  }else{ $tags[]=$result; }
	}
  }
  return $tags;
}

function alink($href,$s='',$target=''){
  return implode(blink($href,$s,$target));
}
function blink($href,$s='',$target='',$class='',$id='',$events=''){
  if(is_array($href)){
    $target=$href[2];
	$s=$href[1];
    $href=$href[0];
  }
  return array('<a href="',$href,'"',
  (bnotempty($target))?' target="'.$target.'">':'>',
  (bnotempty($class))?' class="'.$class.'"':'',
  (bnotempty($id))?' id="'.$id.'"':'',
  (bnotempty($s))?$s:$href,'</a>');
  //$tag=implode('',$link); echo$tag; return$tag;
}
function plink($href,$s='',$target='',$class='',$id='',$events=''){
  echo'<a href="',$href,'"',
  (bnotempty($target))?' target="'.$target.'"':'',
  (bnotempty($class))?' class="'.$class.'"':'',
  (bnotempty($id))?' id="'.$id.'"':'',
  ' ',$events,'>',
  (bnotempty($s))?$s:$href,
  '</a>';
}


function pul($pa,$class='',$id='',$piclass=false,$piid=false){
  echo'<ul',
  (bnotempty($class))?' class="'.$class.'"':'',
  (bnotempty($id))?' id="'.$id.'"':'',
  '>';
  $i=0; foreach($pa as $li){ echo'<li';
    if ($piclass<>false) {
      if (in_array($i,$piclass)){ echo ' class="',$piclass[$i],'"';}}
    if ($piid<>false) {
      if (in_array($i,$piid)){ echo ' id="',$piid[$i],'"'; }}
    echo '>',$li,'</li>';
    ++$i;}
  echo'</ul>';
}

function bul($pa){
    $ul=implode('</li><li>',$pa);
    $tag=array('<ul><li>',$ul,'</li></ul>');
    return $ul;
}


function bol($pa,$class='',$id='',$piclass=false,$piid=false){
  echo'<ol',
  (bnotempty($class))?' class="'.$class.'"':'',
  (bnotempty($id))?' id="'.$id.'"':'',
  '>';
  $i=0; foreach($pa as $li){ echo'<li';
    if ($piclass<>false) {
      if (in_array($i,$piclass)){ echo ' class="',$piclass[$i],'"';}}
    if ($piid<>false) {
      if (in_array($i,$piid)){ echo ' id="',$piid[$i],'"'; }}
    echo '>',$li,'</li>';
    ++$i;}
  echo'</ol>';
}

function bimg($src='',$alt='',$class='',$id='',$style=''){
  if (!bnotempty($src)){ return array();
  }else{
  return array('<img src="',$src,'" alt="',
  (bnotempty($alt))?$alt:$src.' : is unavailable"',
  (bnotempty($class))?' class="'.$class.'"':'',
  (bnotempty($id))?' id="'.$id.'"':'',
  (bnotempty($style))?" style=\"$style\"":'',
  ' />');
  }
}

function pimg($src='',$alt='',$class='',$id='',$style=''){
  if (!bnotempty($src)){ return '';
  }else{
  echo'<img src="',$src,'" alt="',
  (bnotempty($alt))?$alt:$src.' : is unavailable"',
  (bnotempty($class))?' class="'.$class.'"':'',
  (bnotempty($id))?' id="'.$id.'"':'',
  (bnotempty($style))?" style=\"$style\"":'',
  ' />';
  }
}


function byframe($src,$id,$width,$height){
    $iframe=array('<iframe width="',$width,'" height="',$height,'" src="',$src,'" name="',$id,'" id="',$id,'"></iframe>');
    $tag=implode('',$iframe);
    return $tag;
}


function pcanvas($width,$height,$x='',$y='',$class='',$id=''){
  echo'<canvas id="',(bnotempty($id)?$id:'canvas'),'"></canvas>';
}

function pdir($path='.'){
  if($handle=opendir($path)){
    while(false!==($file=readdir($handle))){
      if($file=="." or $file==".."){ echo '';}
      else{echo "$file\n";}
    }
  } closedir($handle); }

function bdir($path='.',$cols=0){
  if($handle=opendir($path)){
    $files=array();
	$count=0;
    while(false!==($file=readdir($handle))){
      if($file<>"." and $file<>".."){
	    $files[]=$file;
      }
	  if($cols>0 and $count==$cols+1){ $count=1; $files[]='<br />'; }
	  ++$count;
    }
	closedir($handle);
	return $files;
  }
}

function pfile($filename,$data,$limit=''){
  $handle=fopen($filename,'w');
  if(is_array($data)){ fwrite($handle,implode($limit,$data)); }
  else{ fwrite($handle,$data); }
  fclose($handle);
  return $filename;
}

function babscrape($url){
  $crl = curl_init();
  $timeout = 5;
  curl_setopt ($crl, CURLOPT_URL,$url);
  curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
  $ret = curl_exec($crl);
  curl_close($crl);
  return $ret;
}

?>
